package com.jxdinfo.hussar.activitidemo.utils.log.thread;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/*
 *线程池设置类
 */
public class LogWritePool {
	
	
	private static int corePoolSize = Integer.parseInt(LogWriteProperties
			.singlton().getProperty("corePoolSize"));// 线程池维护线程的最少数量
	private static int maximumPoolSize = Integer.parseInt(LogWriteProperties
			.singlton().getProperty("maximumPoolSize"));// 线程池维护线程的最大数量
	private static int keepAliveTime = Integer.parseInt(LogWriteProperties
			.singlton().getProperty("keepAliveTime"));// 线程池维护线程所允许的空闲时间,多出corePoolSize之外的线程的允许发呆时间
	private static int queueSize = Integer.parseInt(LogWriteProperties
			.singlton().getProperty("queueSize")); // 1
	private static ThreadPoolExecutor threadPool;
	

	// 先进先出阻塞队列
	private static ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(
			queueSize);
	private static LogWritePool instance = null;

	/*
	 * corePoolSize ： 线程池维护线程的最少数量
	 * 
	 * maximumPoolSize ：线程池维护线程的最大数量
	 * 
	 * keepAliveTime ： 线程池维护线程所允许的空闲时间
	 * 
	 * unit ： 线程池维护线程所允许的空闲时间的单位
	 * 
	 * workQueue ： 线程池所使用的缓冲队列
	 * 
	 * handler ： 线程池对拒绝任务的处理策略
	 */
	public static LogWritePool getInstance() {
		if (instance == null) {
			instance = new LogWritePool();
		}
		return instance;

	}

	public LogWritePool() {
		// 构造一个线程池

		threadPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
				keepAliveTime, TimeUnit.SECONDS, workQueue,
				new ThreadPoolExecutor.CallerRunsPolicy());

	}

	public void shutdown() {
		threadPool.shutdownNow();
	}

	public void execute(Runnable a) {
		threadPool.execute(a);

	}

}

